package com.bzkj.web.controller.docmentprocess;


import com.bzkj.basicinfo.domain.warehouse.PdaWarehouseOutBillsList;
import com.bzkj.basicinfo.service.warehouse.IPdaWarehouseOutBillsService;
import com.bzkj.common.core.controller.BaseController;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.msfx.service.WareHouseOutService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Map;

/**
 * @projectName: btyy
 * @package: com.ruoyi.web.controller.inventoryReceiptPush
 * @className: WareHouseOutController
 * @description: PDA出入库单控制层
 * @author: C1309
 * @createDate: 2024年03月10日
 * @version: v1.0
 */
@RestController
@RequestMapping("/btrh/wareHouse")
public class WareHouseOutController extends BaseController {
    @Resource
    private WareHouseOutService wareHouseOutService;

    @Resource
    private IPdaWarehouseOutBillsService pdaWarehouseOutBillsService;


    /**
     * 查询出入库单列表
     *
     * @param beginDate  查询开始时间
     * @param endDate    查询结束时间
     * @param flag       单据状态
     * @return 查询结果
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('wareHouse:out:list')")
    public AjaxResult queryBillsList(@RequestParam(value = "beginDate", defaultValue = "") String beginDate,
                                     @RequestParam(value = "endDate", defaultValue = "") String endDate,
                                     @RequestParam(value = "flag", defaultValue = "0") String flag,
                                     @RequestParam(value = "userId", required = false, defaultValue = "") String userId,
                                     @RequestParam(value = "billsId", required = false, defaultValue = "") String billsId,
                                     @RequestParam(value = "billsType", required = false) ArrayList<String> billsType) {
        return wareHouseOutService.queryBillsList(beginDate, endDate, flag, userId, billsId, billsType == null ? "" : billsType.get(1));
    }

    /**
     * 查询单据详情
     *
     * @param billsId 单据号
     * @return 查询结果
     */
    @GetMapping("/detail/{billsId}")
    @PreAuthorize("@ss.hasPermi('wareHouse:out:list')")
    public AjaxResult queryBillsDetail(@PathVariable("billsId") String billsId) {
        return wareHouseOutService.queryBillsDetailList(billsId);
    }

    /**
     * 生成xml
     *
     * @param billsIds 单据号
     * @return xml
     */
    @PostMapping("/xml/{billsIds}")
    @PreAuthorize("@ss.hasPermi('wareHouse:out:export')")
    public AjaxResult billsToXml(@PathVariable ArrayList<String> billsIds) {
        return wareHouseOutService.billsToXml(billsIds);
    }

    /**
     * 追溯码替换
     */
    @PostMapping("/changeCode")
    public AjaxResult changeCode(@RequestBody Map<String, String> params) {
        return wareHouseOutService.changeCode(params);
    }

    /**
     * 重做单据
     *
     * @param billsId 单据号
     * @return 结果
     */
    @DeleteMapping("/redo/{billsId}/{businessEntId}")
    @PreAuthorize("@ss.hasPermi('wareHouse:out:delete')")
    public AjaxResult redoBills(@PathVariable("billsId") String billsId,
                                @PathVariable("businessEntId") String businessEntId) {
        return wareHouseOutService.redoBills(billsId, businessEntId);
    }

    /**
     * 查询用户列表
     */
    @GetMapping("/user")
    public AjaxResult getUserList() {
        return  wareHouseOutService.getUserList("1");
    }

    /**
     * 新增出库单据
     *
     * @param billsList 出库单据信息
     * @return 新增结果
     */
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('wareHouse:out:add')")
    public AjaxResult addOutBills(@RequestBody PdaWarehouseOutBillsList billsList) {
        try {
            int result = pdaWarehouseOutBillsService.insertPdaWarehouseOutBillsList(billsList);
            if (result > 0) {
                return AjaxResult.success("新增出库单据成功", billsList.getBillsId());
            } else {
                return AjaxResult.error("新增出库单据失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("新增出库单据异常：" + e.getMessage());
        }
    }

    /**
     * 根据单据号查询出库单据详情（用于回显）
     *
     * @param billsId 单据号
     * @return 单据详情
     */
    @GetMapping("/info/{billsId}")
    @PreAuthorize("@ss.hasPermi('wareHouse:out:query')")
    public AjaxResult getOutBillsInfo(@PathVariable("billsId") String billsId) {
        try {
            PdaWarehouseOutBillsList billsList = pdaWarehouseOutBillsService.selectPdaWarehouseOutBillsByBillsID(billsId);
            if (billsList != null) {
                return AjaxResult.success(billsList);
            } else {
                return AjaxResult.error("未找到单据信息");
            }
        } catch (Exception e) {
            return AjaxResult.error("查询单据详情异常：" + e.getMessage());
        }
    }

}
